/**
 * Created with IntelliJ IDEA.
 * User: aermakov
 * Date: 20.05.13
 * Time: 15:08
 * To change this template use File | Settings | File Templates.
 */

Ext.ns('GrailsCompany');

GrailsCompany.WritableJsonStore = Ext.extend(Ext.data.JsonStore, {
    constructor: function (config) {
        config = Ext.apply({
            autoSave: true,
            autoLoad: true,
            root: 'data'
        }, config);

        GrailsCompany.WritableJsonStore.superclass.constructor.call(this, config);

        if (this.proxy) {
            this.proxy.on('beforewrite', function(proxy, action, rs, params){
                if (action === 'destroy') {
                    params.data = Ext.encode({
                        id: params.data
                    })
                }
            }, this);
        }
    },

    writer: new Ext.data.JsonWriter({
        writeAllFields: true
    })
});

GrailsCompany.DataHelper = Ext.extend(Ext.util.Observable, {
    constructor: function (config) {
        this.loadedCount = 0;
        this.listeners = config.listeners;
        this.addEvents({
            'loadAll': true,
            'update': true
        });

        this.Stores.All = [
            this.Stores.Employee, this.Stores.Project, this.Stores.Department, this.Stores.Organization
        ];

        Ext.each(this.Stores.All, function (it) {
            it.on({
                scope: this,
                load: this.handleStoreLoad,
                add: this.handleStoreUpdate,
                remove: this.handleStoreUpdate,
                update: this.handleStoreUpdate
            });
        }, this);

        GrailsCompany.DataHelper.superclass.constructor.call(this, config);
    },

    handleStoreUpdate: function (store, records, action) {
        this.fireEvent('update', records, action);
    },

    handleStoreLoad: function () {
        this.loadedCount++;
        if (this.loadedCount === this.Stores.All.length) {
            this.fireEvent('loadAll');
        }
    },

    Stores: {
        Employee: new GrailsCompany.WritableJsonStore({
            storeId: 'employee',
            parentStoreId: 'project',
            label: 'Employee',
            proxy: new Ext.data.HttpProxy({
                url: 'employee/list',
                api: {
                    create: 'employee/create',
                    read: 'employee/view',
                    update: 'employee/update',
                    destroy: 'employee/delete'
                }
            }),
            idProperty: 'id',
            fields: ['id', 'firstName', 'lastName', 'emailAddress', 'accountName', {name: 'parentId', type: 'int'}]

        }),
        Project: new GrailsCompany.WritableJsonStore({
            storeId: 'project',
            parentStoreId: 'department',
            childStoreId: 'employee',
            label: 'Project',
            proxy: new Ext.data.HttpProxy({
                url: 'project/list',
                api: {
                    create: 'project/create',
                    read: 'project/view',
                    update: 'project/update',
                    destroy: 'project/delete'
                }
            }),
            idProperty: 'id',
            fields: ['id', 'title', {name: 'parentId', type: 'int'}]
        }),
        Department: new GrailsCompany.WritableJsonStore({
            storeId: 'department',
            parentStoreId: 'organization',
            childStoreId: 'project',
            label: 'Department',
            proxy: new Ext.data.HttpProxy({
                url: 'department/list',
                api: {
                    create: 'department/create',
                    read: 'department/view',
                    update: 'department/update',
                    destroy: 'department/delete'
                }
            }),
            idProperty: 'id',
            fields: ['id', 'title', {name: 'parentId', type: 'int'}]
        }),
        Organization: new GrailsCompany.WritableJsonStore({
            storeId: 'organization',
            childStoreId: 'department',
            label: 'Organization',
            proxy: new Ext.data.HttpProxy({
                url: 'organization/list',
                api: {
                    create: 'organization/create',
                    read: {url: 'organization/view', method: 'GET'},
                    update: 'organization/update',
                    destroy: 'organization/delete'
                }
            }),
            idProperty: 'id',
            fields: ['id', 'title']
        })
    }
});

var DH = new GrailsCompany.DataHelper({
    listeners: {
        'loadAll': function () {
            Ext.getCmp('mainWindow').show();
        }
    }
});